package com.cly.house.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cly.house.dao.JsDao;
import com.cly.house.dao.impl.JsDaoImpl;
import com.cly.house.entity.Myjs;

/**
 * Servlet implementation class JsServlet
 */
@WebServlet("/JsServlet")
public class JsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       JsDao jsDao = new JsDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JsServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String type = request.getParameter("type");
		if(type!=null) {
			switch (type) {
			case "list":
				list(request,response);
				break;
			case "toAdd":
				toAdd(request,response);
				break;
			case "add":
				add(request,response);
				break;
			case "toUpdate":
				toUpdate(request,response);
				break;
			case "update":
				update(request,response);
				break;
			case "delete":
				delete(request,response);
				break;
			default:
				break;
			}
		}
	}
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Myjs> list = jsDao.listAll();
		/* request.setAttribute("list", list); */
		/*
		 * HttpSession session = request.getSession(); session.setAttribute("list",
		 * list);
		 */
		ServletContext application = request.getServletContext();
		application.setAttribute("list", list);
		request.getRequestDispatcher("WEB-INF/jsp/myjs/js_list.jsp").forward(request, response);
		
	}
	protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.getRequestDispatcher("WEB-INF/jsp/myjs/js_add.jsp").forward(request, response);
		
	}
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String jname = request.getParameter("jname");
		Myjs myjs = new Myjs();
		myjs.setJname(jname);
		int count = jsDao.save(myjs);
		if (count>0) {
			response.sendRedirect("JsServlet?type=list");
		}
		
		
	}
	protected void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int jid = Integer.parseInt(request.getParameter("jid")) ;
			Myjs js = jsDao.updateId(jid);
			request.setAttribute("js", js);
			
			request.getRequestDispatcher("WEB-INF/jsp/myjs/js_update.jsp").forward(request, response);
			
		}
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int jid =Integer.parseInt(request.getParameter("jid"));
		String jname = request.getParameter("jname");
		Myjs myjs = new Myjs();
		myjs.setJid(jid);
		myjs.setJname(jname);
		int update = jsDao.update(myjs);
		if (update>0) {
			response.sendRedirect("JsServlet?type=list");
		}
		
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int jid =Integer.parseInt(request.getParameter("jid"));
			boolean delete = jsDao.delete(jid);
			if(delete) {
				response.sendRedirect("JsServlet?type=list");
			}
			
			
		}

}
